package org.szy.plugin.dev.service.impl;


import cn.hutool.core.util.StrUtil;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryColumn;
import com.mybatisflex.core.query.QueryMethods;
import com.mybatisflex.core.query.QueryWrapper;
import org.noear.solon.annotation.Inject;
import org.noear.solon.annotation.ProxyComponent;
import org.szy.core.common.enums.CommonStatusEnum;
import org.szy.core.common.page.QueryPage;
import org.szy.core.common.service.impl.BaseServiceImpl;
import org.szy.plugin.dev.constant.DevMessageUserConstant;
import org.szy.plugin.dev.entity.DevMessageUser;
import org.szy.plugin.dev.mapper.DevMessageUserMapper;
import org.szy.plugin.dev.param.DevMessageUserQueryParam;
import org.szy.plugin.dev.pojo.DevMessageUserPojo;
import org.szy.plugin.dev.service.DevMessageUserService;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

import static org.szy.plugin.dev.entity.table.DevMessageTableDef.DEV_MESSAGE;
import static org.szy.plugin.dev.entity.table.DevMessageUserTableDef.DEV_MESSAGE_USER;

/**
 * 站内消息用户关联Service接口实现类
 *
 * @author wangbuhao
 * @since 2023-06-28 18:17:41
 **/
@ProxyComponent
public class DevMessageUserServiceImpl extends BaseServiceImpl<DevMessageUserMapper, DevMessageUser> implements DevMessageUserService {

    @Inject
    private DevMessageUserMapper devMessageUserMapper;

    @Override
    public QueryWrapper qwByQuery(DevMessageUserQueryParam param) {
        QueryWrapper qw = new QueryWrapper();

        qw.select(DEV_MESSAGE_USER.ID, DEV_MESSAGE.ID.as("messageId"), DEV_MESSAGE.TITLE, DEV_MESSAGE.CATEGORY, DEV_MESSAGE.CONTENT, DEV_MESSAGE_USER.MESSAGE_STATUS, DEV_MESSAGE_USER.MESSAGE_STATE)
                .from(DEV_MESSAGE_USER).innerJoin(DEV_MESSAGE).on(DEV_MESSAGE_USER.MESSAGE_ID.eq(DEV_MESSAGE.ID))
                .and(DEV_MESSAGE_USER.MESSAGE_STATUS.eq(param.getMessageUserStatus()))
                .and(DEV_MESSAGE.STATUS.eq(CommonStatusEnum.ENABLE.getKey()))
                .and(DEV_MESSAGE_USER.MESSAGE_STATE.eq(param.getMessageState()))
                .and(DEV_MESSAGE_USER.RECEIVER_ID.eq(param.getReceiverId()))
                .and(DEV_MESSAGE.CATEGORY.eq(param.getCategory()))
                .and(DEV_MESSAGE.CATEGORY.in(StrUtil.splitTrim(param.getCategory(), ",")))
                .and(DEV_MESSAGE.TITLE.like(param.getTitle()));
        qw.orderBy(DEV_MESSAGE.PUBLISH_TIME.asc());

        return qw;
    }

    @Override
    public Page page(QueryPage page, DevMessageUserQueryParam param) {
        QueryWrapper qw = qwByQuery(param);

        // 正序排序
        if (StrUtil.isNotEmpty(page.getAscField())) qw.orderBy(page.getAscField().split(","));
        // 倒叙排序
        if (StrUtil.isNotEmpty(page.getDescField())) {
            for (String desc : page.getDescField().split(",")) {
                qw.orderBy(new QueryColumn(desc).desc());
            }
        }

        return this.pageAs(page, qw, DevMessageUserPojo.class);
    }

    @Override
    public boolean removeByMessageId(Long id) {
        return remove(DEV_MESSAGE_USER.MESSAGE_ID.eq(id));
    }

    @Override
    public long unRead(String receiverId) {
        QueryWrapper qw = new QueryWrapper();
        qw.and(DEV_MESSAGE_USER.RECEIVER_ID.eq(receiverId)).and(DEV_MESSAGE_USER.MESSAGE_STATUS.eq(DevMessageUserConstant.STATUS_WEIDU));
        return count(qw);
    }

    @Override
    public List<Map> categoryCount(String messageStatus) {
        QueryWrapper qw = new QueryWrapper();
        qw.select(DEV_MESSAGE.CATEGORY, QueryMethods.count(DEV_MESSAGE.ID).as("count"))
                .from(DEV_MESSAGE).innerJoin(DEV_MESSAGE_USER).on(DEV_MESSAGE_USER.MESSAGE_ID.eq(DEV_MESSAGE.ID))
                .and(DEV_MESSAGE_USER.MESSAGE_STATUS.eq(messageStatus))
                .and(DEV_MESSAGE.STATUS.eq(CommonStatusEnum.ENABLE.getKey()))
                .groupBy(DEV_MESSAGE.CATEGORY);
        return listAs(qw, Map.class);
    }

    @Override
    public boolean delete(Long id, Object receiverId) {
        QueryWrapper qw = new QueryWrapper();
        qw.and(DEV_MESSAGE_USER.ID.eq(id)).and(DEV_MESSAGE_USER.RECEIVER_ID.eq(receiverId));

        DevMessageUser user = new DevMessageUser();
        user.setMessageStatus(DevMessageUserConstant.STATUS_SHANCHU);

        return devMessageUserMapper.updateByQuery(user, qw) > 0;
    }

    @Override
    public boolean read(String ids, Object receiverId) {
        QueryWrapper qw = new QueryWrapper();
        qw.and(DEV_MESSAGE_USER.ID.in(StrUtil.splitTrim(ids, ","))).and(DEV_MESSAGE_USER.RECEIVER_ID.eq(receiverId));

        DevMessageUser user = new DevMessageUser();
        user.setReadTime(LocalDateTime.now());
        user.setMessageStatus(DevMessageUserConstant.STATUS_YIDU);

        return devMessageUserMapper.updateByQuery(user, qw) > 0;
    }
}
